package com.ls.markt.controller.admin;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ls.markt.model.admin.EnumManage;
import com.ls.markt.model.admin.EnumManageItem;
import com.ls.markt.service.admin.EnumManageItemService;
import com.ls.markt.service.admin.EnumManageService;
import com.ls.markt.vo.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author ls
 * @descript 枚举管理控制器类
 */
@Controller
@RequestMapping("/enumManage")
public class EnumManageController {

    @Autowired
    private EnumManageService enumManageService;

    @Autowired
    private EnumManageItemService enumManageItemService;

    @GetMapping("/index")
    public String index(){
        return "admin/enumManage/index";
    }

    @RequestMapping("/list")
    @ResponseBody
    public JSONObject list(){
        List<EnumManage> enumManages = enumManageService.listEnumManages();
        JSONObject result = new JSONObject();
        result.put("total",enumManages.size());
        result.put("rows",enumManages);
        return result;
    }

    @GetMapping("/add")
    public String add(){
        return "admin/enumManage/add";
    }

    @GetMapping("/edit/{id}")
    public ModelAndView edit(@PathVariable("id")Long id, Model model){
        EnumManage enumManage = enumManageService.getEnumManageById(id);
        model.addAttribute("enumManage",enumManage);
        return new ModelAndView("admin/enumManage/edit","enumManageModel",model);
    }

    /**
     * 保存或更新枚举
     * @param enumManage
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public Response save(EnumManage enumManage){
        enumManageService.saveEnumManage(enumManage);
        return new Response(true);
    }

    /**
     * 执行删除枚举
     * @param id
     * @return
     */
    @DeleteMapping("/doDelete/{id}")
    @ResponseBody
    public Response doDelete(@PathVariable("id")Long id){
        List<EnumManageItem> enumManageItems = enumManageItemService.listItemsByEnumId(id);
        if (enumManageItems.size()>0){
            for (EnumManageItem item: enumManageItems) {
                enumManageItemService.removeEnumManageItem(item.getId());
            }
        }
        enumManageService.removeEnumManage(id);
        return new Response(true);
    }

    /**
     * 根据枚举名称 获取该枚举下的值列表
     * @param enumName
     * @return
     */
    @RequestMapping("/getEnumTree")
    @ResponseBody
    public JSONArray getEnumTree(@RequestParam(value = "enumName",required = true)String enumName){
        JSONArray jsonTree = new JSONArray();
        Long enumManageId = enumManageService.getIdByName(enumName);
        List<EnumManageItem> enumManageItems = enumManageItemService.listItemsByEnumId(enumManageId);
        for (EnumManageItem enumManageItem:enumManageItems){
            JSONObject jNode = new JSONObject();
            jNode.put("id",enumManageItem.getId());
            jNode.put("text",enumManageItem.getName());
            jsonTree.add(jNode);
        }
        return jsonTree;
    }
}
